package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    @PostMapping("loginReginfo")
    public ResponseEntity<ErrorResult> saveUserInfo(@RequestHeader("authorization") String token,@RequestBody Map<String,String> param){

        try {
            Boolean Result = userInfoService.saveUserInfo(param, token);
            if (Result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.builder().errCode("000001").errMessage("保存用户信息失败").build());
    }

    @PostMapping("loginReginfo/head")
    public ResponseEntity<ErrorResult> uploadUserLogo(@RequestHeader("Authorization") String token, @RequestParam("headPhoto") MultipartFile file){
        try {
            Boolean result = userInfoService.saveUserLogo(token,file);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.builder().errCode("000001").errMessage("保存用户logo失败").build());
    }
}
